﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmLop_Sua : Form
    {
        private static frmLop_Sua frm = null;
        private Lop _lop = null;
        private Lop _lopOld = new Lop();

        public event UpdateLop UpdateLopSuccess = null;

        private frmLop_Sua()
        {
            InitializeComponent();
            cmbBoMon.DataSource = (new BoMon()).ListName();
        }

        private frmLop_Sua(Lop lop)
        {
            InitializeComponent();
            cmbBoMon.DataSource = (new BoMon()).ListName();
            this._lop = lop;
            this._lopOld.ThongTin.Ma = lop.ThongTin.Ma;
            this._lopOld.ThongTin.Ten = lop.ThongTin.Ten;
            this._lopOld.ThongTin.BoMon = lop.ThongTin.BoMon;
        }

        public static frmLop_Sua Create()
        {
            if (frm == null)
            {
                frm = new frmLop_Sua();
            }
            frm.UpdateData();
            return frm;
        }

        public static frmLop_Sua Create(Lop lop)
        {
            if (frm == null)
            {
                frm = new frmLop_Sua(lop);
            }
            frm._lop = lop;
            frm._lopOld.ThongTin.Ma = lop.ThongTin.Ma;
            frm._lopOld.ThongTin.Ten = lop.ThongTin.Ten;
            frm._lopOld.ThongTin.BoMon = lop.ThongTin.BoMon;
            frm.UpdateData();
            return frm;
        }

        private void UpdateData()
        {
            txtMa.Text = _lop.ThongTin.Ma.ToString();
            txtTen.Text = _lop.ThongTin.Ten;
            cmbBoMon.Text = _lop.ThongTin.BoMon;
        }

        private void frmLop_Sua_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTen.Text.Trim()))
            {
                MessageBox.Show("Phải nhập tên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                _lop.ThongTin.Ten = txtTen.Text.Trim();
                _lop.ThongTin.BoMon = cmbBoMon.Text;
                if (_lop.ThongTin.Ten == _lopOld.ThongTin.Ten && _lop.ThongTin.BoMon == _lopOld.ThongTin.BoMon)
                {
                    this.Close();
                }
                else
                {
                    if (_lop.Update() > 0)
                    {
                        InfoUpdateLop info = new InfoUpdateLop();
                        info.Lop = this._lop;
                        UpdateLopSuccess(this, info);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Có vấn đề khi sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }
    }
}
